﻿using System.Collections;
using System.Collections.Generic;
using DD;
using MoreMountains.NiceVibrations;

using UnityEngine;
using UnityEngine.SceneManagement;

public delegate void GotItem(ItemType _itemType);


public class Ball : MonoBehaviour
{
    public static event GotItem OnGotItemHandller;


    //anim
    public Animator[] BallAnims;
    //rigi
    public Rigidbody BallRB;
    //camera
    public CameraFollow Camera;
    //特效
    public GameObject SplashFX;
    //over面板
    public GameObject GameOverPanel;
    //音樂組件
    public AudioSource Audio;

    //落地音效
    public AudioClip BoltAudio;
    //穿過障礙音效
    public AudioClip CoinAudio;
    //力度
    public float ForceValue;
    public float G;

    //穿越平台的次数(大于三次时下一次碰到什么平台都销毁)
    private int m_througCount;

    private TestSphere sphere;
    private int _tailNumber = 0;



    [Range(300, 1000)]
    [SerializeField] private int ExForce;


    [HideInInspector]
    public bool IsFly;
    public static bool IsGameOver;

    private bool _setUpvelocity;

    private long[] Long = new long[] { 7, 50, 7, 50, 7 };
    private int[] Int = new int[] { 5, 0, 5, 0, 5 };

    private void Start()
    {

        IsFly = false;
        IsGameOver = false;
        Physics.gravity = new Vector3(0, -20f, 0);
        sphere = this.GetComponent<TestSphere>();
    }

    private void FixedUpdate()
    {
        if (_setUpvelocity)
        {
            if (!Audio.isPlaying)
            {
                Audio.PlayOneShot(BoltAudio);
            }
            //BallAnim.SetTrigger("Jump");
            foreach (Animator animator in BallAnims)
            {
                animator.SetTrigger("Jump");
            }
            BallRB.velocity = new Vector3(0f, 8f, 0f);
            _setUpvelocity = false;
            IsFly = false;
        }
        BallRB.velocity += Vector3.up * Time.fixedDeltaTime * 2;
        //else if(BallRB.velocity.y<=0)
        //{
        //    BallRB.velocity = Vector3.down * Time.fixedDeltaTime*Time.fixedDeltaTime * G;

        //}
    }

    private void OnCollisionEnter(Collision other)
    {
        CameraFollow.m_currentMomentBollPos = transform.position;

        if (m_througCount >= 3)
        {
            ScoreManager.Instance.AddScore();
            if (!Audio.isPlaying)
            {
                Audio.PlayOneShot(CoinAudio);
            }
            other.transform.tag = "Diao";

            foreach (Transform Trans in other.transform)
            {
                Trans.gameObject.AddComponent<Rigidbody>().AddExplosionForce(ExForce, other.transform.position, 0, 0.2f, ForceMode.Force);
                if (Trans.GetComponent<Jelly>() != null)
                {
                    Trans.GetComponent<Jelly>().enabled = false;
                }

                if (Trans.GetComponent<MeshCollider>() != null)
                {
                    Trans.GetComponent<MeshCollider>().isTrigger = true;

                }
            }

            _setUpvelocity = true;
            GameObject particle = Instantiate(SplashFX, other.transform, false);
            particle.transform.position = new Vector3(other.transform.position.x, other.transform.position.y + 0.01f, other.transform.position.z - 1.5f);
            particle.transform.rotation = Quaternion.Euler(90f, 0, Random.Range(0, 360f));

            m_througCount = 0;
            IsFly = true;
            MMVibrationManager.AndroidVibrate(2);
        }
        else
        {
            if (other.collider.tag == "Fail")
            {
                MMVibrationManager.AndroidVibrate(Long, Int, -1);
                Fail();
            }
            else
            {
                MMVibrationManager.AndroidVibrate(2);
                //碰到普通平台要重置
                m_througCount = 0;
                //if ( other.contacts[0].point.y + 0.1f > other.transform.position.y )
                //{
                _setUpvelocity = true;
                GameObject particle = Instantiate(SplashFX, other.transform, false);
                particle.transform.position = new Vector3(other.transform.position.x, other.transform.position.y + 0.01f, other.transform.position.z - 1.5f);
                particle.transform.rotation = Quaternion.Euler(90f, 0, Random.Range(0, 360f));
                //}

            }
        }

    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Item_Force")
        {
            OnGotItemHandller?.Invoke(ItemType.ADDFORCE);
            Destroy(other.gameObject);
            BallRB.velocity = Vector3.zero;
            m_througCount = 3;
        }
        else if (other.gameObject.tag == "Item_Boom")
        {
            OnGotItemHandller?.Invoke(ItemType.BOOM);
            Destroy(other.gameObject);
            m_througCount = 3;
        }
        else if (other.gameObject.tag == "Item")
        {
            ScoreManager.Instance.AddScore();
            if (_tailNumber <= 8)
            {
                sphere.OnAddTail();
            }
            Destroy(other.gameObject);
        }
        else if (other.gameObject.tag == "HelixPlatform")
        {
            m_througCount++;

            ScoreManager.Instance.AddScore();
            if (!Audio.isPlaying)
            {
                Audio.PlayOneShot(CoinAudio);
            }
            other.tag = "Diao";
            foreach (Transform Trans in other.transform)
            {
                Trans.gameObject.AddComponent<Rigidbody>().AddExplosionForce(ExForce, other.transform.position, 0, 0.2f, ForceMode.Force);
                if (Trans.GetComponent<Jelly>() != null)
                {
                    Trans.GetComponent<Jelly>().enabled = false;
                }

                if (Trans.GetComponent<MeshCollider>() != null)
                {
                    Trans.GetComponent<MeshCollider>().isTrigger = true;

                }
            }
            IsFly = true;
        }
    }

    private void Fail()
    {
        IsGameOver = true;
        ScoreManager.Instance.SetBestScore();
        GameOverPanel.SetActive(true);
        Time.timeScale = 0;
    }

    public void Restert()
    {
        SceneManager.LoadScene("Main");
    }
}
